package com.eastfair.home.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.exception.BizException;
import com.eastfair.echo.core.EchoService;
import com.eastfair.home.dto.ReportVenueSpaceDTO;
import com.eastfair.home.dto.ReportVenueSpacePageQuery;
import com.eastfair.home.dto.ReportVenueSpaceSaveDTO;
import com.eastfair.home.dto.ReportVenueSpaceUpdateDTO;
import com.eastfair.home.entity.ReportVenueSpace;
import com.eastfair.home.service.ReportVenueSpaceService;
import com.eastfair.home.vo.ReportVenueSpaceVO;
import com.eastfair.venueservice.dto.ServiceBookingDTO;
import com.eastfair.venueservice.dto.ServiceBookingQuery;
import com.eastfair.venueservice.vo.ServiceBookingVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * <p>
 * 前端控制器
 * 场馆一键报馆记录
 * </p>
 *
 * @author dq
 * @date 2022-09-07
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/reportVenueSpace")
@Api(value = "ReportVenueSpace", tags = "场馆一键报馆记录")
@PreAuth(replace = "home:reportVenueSpace:")
public class ReportVenueSpaceController extends SuperController<ReportVenueSpaceService, Long, ReportVenueSpace, ReportVenueSpacePageQuery, ReportVenueSpaceSaveDTO, ReportVenueSpaceUpdateDTO> {

    @Resource
    private EchoService echoService;

    @Resource
    private ReportVenueSpaceService reportVenueSpaceService;

    @Override
    public void handlerResult(IPage<ReportVenueSpace> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }

    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list) {
        List<ReportVenueSpace> reportVenueSpaceList = list
                .stream()
                .map((map) -> ReportVenueSpace.builder().build())
                .collect(Collectors.toList());
        return R.success(baseService.saveBatch(reportVenueSpaceList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<ReportVenueSpacePageQuery> params) {
        ReportVenueSpacePageQuery pageQuery = params.getModel();
        //分页处理
        if (ContextUtil.getProjectId() != null) {
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 分页查询一键报馆记录VO列表.
     *
     * @param pageParams the page params
     * @return the r
     */
    @ApiOperation("分页查询一键报馆记录VO列表")
    @PostMapping("/query/reportVenueSpace")
    public R<IPage<ReportVenueSpaceVO>> queryReportVenueSpace(@RequestBody PageParams<ReportVenueSpaceDTO> pageParams) {
        log.info("queryReportVenueSpace - 分页查询一键报馆记录VO列表, pageParams={}", pageParams);
        IPage<ReportVenueSpaceVO> page = reportVenueSpaceService.listReportVenueSpaceVOs(pageParams);
        return R.successDef(page);
    }

    /**
     * id查询一键报馆记录.
     *
     * @param id the id
     * @return the by id
     */
    @ApiOperation("id查询一键报馆记录")
    @GetMapping("/get/{id}")
    public R<ReportVenueSpaceVO> getById(@PathVariable("id") Long id) {
        log.info("getById - id查询一键报馆记录, id={}", id);
        ReportVenueSpaceVO reportVenueSpaceVO = reportVenueSpaceService.getReportVenueSpaceVOById(id);
        return R.successDef(reportVenueSpaceVO);
    }

    /**
     * 提交一键报馆.
     *
     * @param reportVenueSpaceNumber the report venue space number
     * @return the r
     */
    @ApiOperation("提交一键报馆")
    @GetMapping("/submit/{reportVenueSpaceNumber}")
    public R<Boolean> submit(@PathVariable("reportVenueSpaceNumber") String reportVenueSpaceNumber) {
        log.info("submit - 提交一键报馆, reportVenueSpaceNumber={}", reportVenueSpaceNumber);
        return R.successDef(reportVenueSpaceService.submitReportVenueSpace(reportVenueSpaceNumber));
    }

    /**
     * 保存一键报馆.
     *
     * @param reportVenueSpaceDTO the report venue space dto
     * @return the r
     */
    @ApiOperation("保存一键报馆")
    @SysLog("添加一键报馆")
    @PostMapping("/save")
    public R<ReportVenueSpace> save(@RequestBody ReportVenueSpaceDTO reportVenueSpaceDTO) {
        log.info("save - 保存一键报馆, reportVenueSpaceDTO={}", reportVenueSpaceDTO);
        return R.successDef(reportVenueSpaceService.saveReportVenueSpace(reportVenueSpaceDTO));
    }

    /**
     * 更新一键报馆.
     *
     * @param reportVenueSpaceDTO the report venue space dto
     * @return the r
     */
    @ApiOperation("更新一键报馆")
    @SysLog("修改一键报馆")
    @PostMapping("/update")
    public R<ReportVenueSpace> update(@RequestBody ReportVenueSpaceDTO reportVenueSpaceDTO) {
        log.info("update - 更新一键报馆, reportVenueSpaceDTO={}", reportVenueSpaceDTO);
        return R.successDef(reportVenueSpaceService.updateReportVenueSpace(reportVenueSpaceDTO));
    }

    /**
     * 更新一键报馆审核状态.
     *
     * @param reportVenueSpaceDTO the report venue space dto
     * @return the r
     */
    @ApiOperation("更新一键报馆审核状态")
    @PostMapping("/update/auditStatus")
    public R<Boolean> updateAuditStatus(@RequestBody ReportVenueSpaceDTO reportVenueSpaceDTO) {
        log.info("updateAuditStatus - 更新一键报馆审核状态, reportVenueSpaceDTO={}", reportVenueSpaceDTO);
        return R.successDef(reportVenueSpaceService.updateAuditStatus(reportVenueSpaceDTO));
    }

    /**
     * 批量删除一键报馆.
     *
     * @param ids the ids
     * @return the r
     */
    @ApiOperation("批量删除一键报馆")
    @SysLog("删除一键报馆")
    @PostMapping("/remove")
    public R<Boolean> deleteByIds(@RequestBody List<Long> ids) {
        log.info("deleteByIds - 批量删除一键报馆, ids={}", ids);
        return R.successDef(reportVenueSpaceService.removeReportVenueSpace(ids));
    }

    /**
     * 主场一键报馆的全部服务
     *
     * @param serviceBookingQuery 主场一键报馆的全部服务
     * @return R
     */
    @ApiOperation(value = "主场一键报馆的全部服务", notes = "主场一键报馆的全部服务")
    @PostMapping("/queryServiceBookingOfHome")
    public R<Page<ServiceBookingVO>> queryServiceBookingOfHome(@RequestBody PageParams<ServiceBookingQuery> serviceBookingQuery){
        log.info("queryServiceBookingOfHome - 主场一键报馆的全部服务 serviceBookingQuery={}", serviceBookingQuery);
        return R.success(reportVenueSpaceService.queryServiceBookingOfHome(serviceBookingQuery));
    }

    /**
     * 主场一键报馆服务(保存)
     * @param serviceBookingDTO
     * @return R<Boolean>
     */
    @ApiOperation(value = "主场一键报馆服务")
    @PostMapping(value = "/submitServiceBookingToVenue")
    public R<Boolean> submitServiceBookingToVenue(@RequestBody List<ServiceBookingDTO> serviceBookingDTO) {
        log.info("submitServiceBookingToVenue - 主场一键报馆服务 serviceBookingDTO={}", serviceBookingDTO);
        try {
            return R.success(baseService.submitServiceBookingToVenue(serviceBookingDTO));
        } catch(BizException biz) {
            return R.fail(biz);
        }
    }

    /**
     * 客展搭建商报馆导入.
     *
     * @param file the multipart file
     * @return the r
     * @throws Exception the exception
     */
    @PostMapping("/guestExhibitionImport")
    public R<Boolean> guestExhibitionImport(@RequestPart("file") MultipartFile file,@RequestParam("exhibitionManageId") Long  exhibitionManageId,@RequestParam("homeId") Long  homeId,@RequestParam("spaceName") String  spaceName) throws Exception {
        log.info("guestExhibitionImport - 客展搭建商报馆导入");
        baseService.checkImport(file,exhibitionManageId,homeId,spaceName);
        return R.successDef();
    }
}
